package com.chendx.store.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.goods.dto.TemplateDto;
import com.chendx.model.goods.pojo.*;
import com.chendx.model.goods.vo.TemplateVo;
import com.chendx.store.goods.mapper.*;
import com.chendx.store.goods.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by chendx on 2022/5/19.
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TbTemplateMapper,Template> implements TemplateService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private TbTemplateMapper templateMapper;
    @Autowired
    private CategoryTemplateMapper categoryTemplateMapper;

    @Override
    public ResponseResult templateInfos() {
        List<Template> templateList = templateMapper.selectList(null);
        List<TemplateVo> templateVos = new ArrayList<>();
        for (Template template : templateList){
            List<Brand> brandList = new ArrayList<>();
            TemplateVo templateVo = new TemplateVo();
            templateVo.setId(template.getId());
            templateVo.setName(template.getName());
            List<Category> categoryList = categoryMapper.selectList(Wrappers.<Category>lambdaQuery()
                    .eq(Category::getTemplateId, template.getId())
                    .eq(Category::getLevel,3));
            templateVo.setCategories(categoryList);
            for (Category category : categoryList){
                List<Integer> brandIds = categoryBrandMapper.getBrandIds(category.getId());
                if (brandIds.size() > 0){
                    for (Integer id : brandIds){
                        Brand brand = brandMapper.selectById(id);
                        brandList.add(brand);
                    }
                }
            }
            List<Brand> brands =  removeDupliById(brandList);
            templateVo.setBrands(brands);
            List<Spec> specList = specMapper.selectList(Wrappers.<Spec>lambdaQuery().eq(Spec::getTemplateId, template.getId()));
            templateVo.setSpecs(specList);
            templateVos.add(templateVo);
        }
        return ResponseResult.okResult(templateVos);
    }

    private List<Brand> removeDupliById(List<Brand> brands){
        Set<Brand> brandSet = new TreeSet<>((o1,o2) -> o1.getId().compareTo(o2.getId()));
        brandSet.addAll(brands);
        return new ArrayList<>(brandSet);
    }

    @Override
    public ResponseResult oneinfo(Integer id) {
        Template template = getById(id);
        TemplateVo templateVo = new TemplateVo();
        templateVo.setId(template.getId());
        templateVo.setName(template.getName());
        List<Category> categoryList = categoryMapper.selectList(Wrappers.<Category>lambdaQuery()
                .eq(Category::getTemplateId, template.getId())
                .eq(Category::getLevel,3));
        templateVo.setCategories(categoryList);
        List<Brand> brandList = new ArrayList<>();
        for (Category category : categoryList){
            brandList = getBrandList(brandList,category.getId());
            templateVo.setBrands(brandList);
        }
        List<Spec> specList = specMapper.selectList(Wrappers.<Spec>lambdaQuery().eq(Spec::getTemplateId, template.getId()));
        templateVo.setSpecs(specList);
        return ResponseResult.okResult(templateVo);
    }

    @Override
    @Transactional
    public ResponseResult add(TemplateDto dto) {
        if (dto == null || dto.getName() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Template template = new Template();
        template.setName(dto.getName());
        template.setSpecNum(dto.getSpecIds().size());
        save(template);

        List<Integer> categoryIds = dto.getCategoryIds();
        for (Integer categoryId : categoryIds){
            Category category = new Category();
            category.setId(categoryId);
            category.setTemplateId(template.getId());
            categoryMapper.updateById(category);
        }

        List<Integer> specIds = dto.getSpecIds();
        for (Integer specId : specIds){
            Spec spec = new Spec();
            spec.setId(specId);
            spec.setTemplateId(template.getId());
            specMapper.updateById(spec);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private List<Brand> getBrandList(List<Brand> brandList,Integer categoryId){
        List<Integer> brandIds = categoryBrandMapper.getBrandIds(categoryId);
        if (brandIds.size() > 0){
            for (Integer brandId : brandIds){
                Brand brand = brandMapper.selectById(brandId);
                brandList.add(brand);
            }
        }
        return brandList;
    }
}
